// 获取所有的emoji

const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
// 目标URL
const url = "https://www.emojiall.com/zh-hans/all-emojis";

// 初始化Emoji树结构
let emojiTree = {
  categories: [],
};

axios
  .get(url)
  .then((response) => {
    const html = response.data;
    const $ = cheerio.load(html);

    // 解析分类
    $(".emoji_list_box").each((index, element) => {
      $(element)
        .find("h2")
        .each((index, element) => {
          let category = {};
          $(element)
            .find("a")
            .contents()
            .each((index, content) => {
              // 检查内容是否是文本节点
              if (
                content.type === "text" &&
                !$(content).parent().is("span, sup")
              ) {
                // 输出<span>和<sup>标签外的文本内容
                category = {
                  char: $(element).find(".emoji_font").text().trim(),
                  title: $(content).text().trim(),
                  num: $(element).find("sup").text().trim(),
                  emojis: [],
                };
              }
            });

          // 解析子分类
          $(element)
            .nextUntil("h2")
            .each((index, element) => {
              if ($(element).is("h3")) {
                let subcategory = {};
                $(element)
                  .find("a")
                  .contents()
                  .each((index, content) => {
                    // 检查内容是否是文本节点
                    if (
                      content.type === "text" &&
                      !$(content).parent().is("span, sup")
                    ) {
                      // 输出<span>和<sup>标签外的文本内容
                      subcategory = {
                        char: $(element).find(".emoji_font").text().trim(),
                        title: $(content).text().trim(),
                        num: $(element).find("sup").text().trim(),
                        emojis: [],
                      };
                    }
                  });

                $(element)
                  .nextUntil("h3")
                  .each((index, element) => {
                    if ($(element).is("div")) {
                      $(element)
                        .find(".emoji_card")
                        .each((emojiIndex, element) => {
                          const emojiChar = $(element)
                            .find(".emoji_font")
                            .text()
                            .trim(); // 获取emoji字符

                          const emojiName = $(element)
                            .find(".emoji_name")
                            .text()
                            .trim(); // 获取emoji名称

                          subcategory.emojis.push({
                            char: emojiChar,
                            name: emojiName,
                          });
                        });
                    }
                  });

                category.emojis.push(subcategory);
              }
            });

          emojiTree.categories.push(category);
        });
    });

    // 将结果转换为JSON字符串并写入文件
    const jsonData = JSON.stringify(emojiTree, null, 2);
    fs.writeFileSync("data/emoji_tree.json", jsonData);

    console.log("已成功抓取并保存到emoji_tree.json");
  })
  .catch((error) => {
    console.error("抓取过程中出错:", error);
  });
